/*
 * Su-yun Information Technology, Co.,Ltd CONFIDENTIAL
 *
 * (C) Copyright Su-yun Information Technology, Co.,Ltd. 2021 All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains the property of
 * Su-yun Information Technology, Co.,Ltd. The intellectual property herein
 * are protected by trade secret, patent law or copyright law. Dissemination
 * of this information or reproduction of this material is strictly forbidden
 * unless prior written permission is obtained from Su-yun Information Technology.
 *
 * https://suyunplat.com/
 *
 */
package com.xtiim.battery.discharge;

import com.xtiim.battery.discharge.common.Configuration;
import com.xtiim.battery.discharge.common.Constants;
import com.xtiim.battery.discharge.modules.htm.controller.HostInfoController;
import com.xtiim.battery.discharge.protocal.manager.CommandManager;
import com.xtiim.battery.discharge.protocal.manager.CommunicationManager;
import com.xtiim.battery.discharge.protocal.manager.HostManager;
import com.xtiim.battery.discharge.server.JettyServer;
import lombok.extern.slf4j.Slf4j;

/**
 * Main application
 *
 */
@Slf4j
public class Application {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        log.info("Application begin to start.");
        initialize();
        log.info("Application is started. Take {} mill seconds.", (System.currentTimeMillis() - start));
    }

    /**
     * Initialize jetty server, load configuration and application environment.
     *
     */
    public static void initialize() {
        try {
            Configuration.load("application.properties");
        } catch (Exception e) {
            e.printStackTrace();
        }
        int jettyPort = Integer.parseInt(Configuration.get(Constants.HTTP_PORT, Constants.HTTP_PORT_DEFAULT));
        JettyServer.start(jettyPort);
        log.info("Jetty started on {}", jettyPort);
        registerServlet();

        HostManager.getInstance().init();
        CommunicationManager.getInstance().init();
        CommandManager.getInstance().init();


    }

    /**
     * Register servlet into web container
     *
     */
    private static void registerServlet() {
        int port = Integer.parseInt(Configuration.get(Constants.HTTP_PORT, Constants.HTTP_PORT_DEFAULT));
        JettyServer.addServlet(port, "/htm/host_info/*", new HostInfoController());

    }
}
